using System;
using System.Drawing;

using Microsoft.DirectX.Direct3D;
using SharpWind.Tools;

namespace SharpWind.Resources
{
	public class Font : IDisposable
	{
		private Microsoft.DirectX.Direct3D.Font font = null;

		private DrawTextFormat format = DrawTextFormat.None;

		public Font( string face, float size ) : this( face, size, false, false )
		{

		}

		public Font( string face, float size, bool bold, bool italic )
		{
			FontStyle style = 0;

			if( bold )
			{
				style |= FontStyle.Bold;
			}

			if( italic )
			{
				style |= FontStyle.Italic;
			}

			this.font =
				new Microsoft.DirectX.Direct3D.Font( Engine.Instance.Renderer.Device, new System.Drawing.Font( face, size, style ) );
		}

		internal Rectangle MeasureString( string text, Color color )
		{
			return this.font.MeasureString( Engine.Instance.Renderer.TextSprite, text, this.format, color );
		}

		internal void DrawText( string text, Rectangle rectangle, DrawTextFormat format, Color color )
		{
			this.format = format;

			Engine.Instance.Renderer.TextSprite.Begin( SpriteFlags.AlphaBlend );

			this.font.DrawText( Engine.Instance.Renderer.TextSprite, text, rectangle, format, color );

			Engine.Instance.Renderer.TextSprite.End();
		}

		public void Dispose()
		{
			SafeDispose.Dispose( this.font );
		}
	}
}
